/*
 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
 * Licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain g_longVal1 copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <tee_ext_api.h>
#include <tee_log.h>
#include <securec.h>
#include <tee_arith_api.h>
#include <tee_mem_mgmt_api.h>
#include "test_arithmetic_api_base.h"

static uint8_t g_longVal1[] = {
    0x6A, 0xA2, 0xC3, 0xD4, 0x94, 0xFD, 0xB7, 0xE2, 0xF0, 0xFC, 0x91, 0x72, 0xC1, 0x50, 0x2A, 0x2C,
    0x9A, 0xA3, 0x71, 0xF0, 0x92, 0xC0, 0xA3, 0xC9, 0x00, 0xC3, 0x51, 0xC8, 0x9D, 0x7B, 0x06, 0x7C,
    0x94, 0x4F, 0x30, 0x49, 0x92, 0x5D, 0xB4, 0x27, 0xBA, 0x58, 0x1D, 0xFC, 0xF5, 0x83, 0xAA, 0xDD,
    0x54, 0x02, 0x64, 0x22, 0x6A, 0x4E, 0xD0, 0x77, 0xA6, 0xE8, 0xEB, 0x39, 0x13, 0xE3, 0x42, 0x6B,
    0xDF, 0x75, 0x08, 0xAC, 0xE9, 0xB0, 0xAE, 0x61, 0x31, 0x90, 0xDD, 0x02, 0x77, 0xF1, 0x88, 0x89,
    0x79, 0x90, 0x6C, 0xD8, 0x5A, 0x8B, 0x1A, 0x5B, 0x13, 0xE9, 0x80, 0xC0, 0x50, 0xDA, 0x8F, 0x71,
    0x8F, 0x64, 0x44, 0xFA, 0xDB, 0xDC, 0x1F, 0x95, 0x2E, 0x88, 0x73, 0x3F, 0x75, 0x8D, 0x71, 0x6B,
    0x8F, 0xF6, 0x7E, 0x57, 0xC3, 0x6B, 0x9B, 0xD3, 0xCA, 0x01, 0x25, 0xFC, 0x0C, 0xD2, 0xFB, 0x4F,
    0x93, 0x05, 0xA0, 0x6B, 0xFA, 0x2F, 0xA2, 0x51, 0x35, 0xEF, 0x13, 0xBD, 0x81, 0x7A, 0x88, 0x07,
    0x7C, 0xA2, 0xB8, 0x0F, 0xD1, 0xC2, 0xF6, 0x9A, 0x66, 0x77, 0xA4, 0x5F, 0xE8, 0x6A, 0x5B, 0x1B,
    0x93, 0x05, 0x7C, 0x4F, 0xD5, 0xDA, 0x66, 0xA8, 0x52, 0x80, 0x5B, 0xB3, 0xB6, 0x27, 0xEC, 0x1B,
    0x35, 0xD2, 0x7C, 0x67, 0x2A, 0x33, 0xD0, 0xDF, 0x83, 0x74, 0x11, 0xF5, 0xFF, 0xE4, 0x21, 0x8E,
    0x3D, 0x6E, 0x4F, 0x43, 0x4F, 0x86, 0x55, 0x32, 0x27, 0x99, 0x94, 0x6E, 0x70, 0xA4, 0x51, 0x5E,
    0xD9, 0x2D, 0x5C, 0x91, 0x0E, 0x2F, 0x5F, 0xF7, 0x16, 0xBA, 0x71, 0xBD, 0x95, 0x5D, 0x89, 0xF5,
    0x01, 0x7F, 0x00, 0xEB, 0x30, 0x81, 0x22, 0x23, 0xCE, 0x21, 0x69, 0xAB, 0x16, 0xBC, 0x25, 0x98,
    0x8E, 0x52, 0x32, 0x06, 0xC0, 0x22, 0xAD, 0xEA, 0x49, 0xF9, 0xCA, 0xE0, 0x73, 0x71, 0x02, 0xA5,
};

static uint8_t g_longVal2[] = {
    0x71, 0x1A, 0x84, 0x69, 0x8E, 0x93, 0x53, 0x79, 0x90, 0x63, 0x80, 0x40, 0x05, 0x95, 0xEC, 0x2F,
    0xFC, 0x6E, 0x7D, 0xD4, 0x10, 0xF7, 0x68, 0x1B, 0x1A, 0xB7, 0x3D, 0x83, 0x38, 0x3E, 0x84, 0xB8,
    0x9C, 0x4A, 0x6F, 0x04, 0xB3, 0x4D, 0x3E, 0x9D, 0x57, 0xF5, 0xE6, 0x42, 0x43, 0xEA, 0x9E, 0x5D,
    0x65, 0xAF, 0x59, 0x63, 0xC4, 0xAE, 0xD6, 0xAA, 0x9F, 0x9D, 0x7B, 0x5A, 0x75, 0xB0, 0x21, 0xAE,
    0x99, 0xA6, 0xCE, 0x40, 0x39, 0x14, 0xEA, 0x43, 0x3B, 0x08, 0xB9, 0x7D, 0xCB, 0x95, 0x3C, 0x32,
    0xA4, 0x99, 0xE9, 0x3E, 0xDF, 0xC2, 0x27, 0x9F, 0x53, 0xAA, 0xB5, 0xB7, 0x99, 0xA3, 0xB5, 0xF5,
    0xA7, 0x02, 0x31, 0x20, 0x71, 0x0D, 0x73, 0x4F, 0xA3, 0x5E, 0x44, 0x94, 0x7F, 0x08, 0x4D, 0x13,
    0x58, 0xED, 0x03, 0x69, 0xFB, 0xBE, 0x5D, 0xE5, 0x23, 0xA4, 0x0D, 0xFC, 0x05, 0xB3, 0xA1, 0x23,
    0x18, 0x58, 0x1D, 0x03, 0xE9, 0xDE, 0xEC, 0x13, 0x8C, 0x44, 0x35, 0x50, 0x6F, 0x5D, 0x8E, 0xA5,
    0xF3, 0xB1, 0xF7, 0x76, 0x07, 0x4E, 0xC7, 0x49, 0x85, 0x13, 0xA1, 0xD8, 0x3C, 0xAF, 0x77, 0x2D,
    0xC7, 0x9E, 0x8E, 0x8F, 0x16, 0xA8, 0x9E, 0x72, 0x7A, 0xC8, 0xF3, 0x39, 0x81, 0x66, 0xCC, 0xB4,
    0x07, 0x02, 0xE1, 0x1E, 0x11, 0xA1, 0x37, 0x22, 0x8F, 0xA5, 0xF3, 0x12, 0xBE, 0x79, 0xFC, 0x20,
    0x71, 0x26, 0x3D, 0x6B, 0x87, 0xC1, 0xCF, 0x1D, 0x62, 0xCB, 0xD8, 0x96, 0x0E, 0x11, 0xB5, 0x08,
    0x37, 0x64, 0xBF, 0xB3, 0xF2, 0x62, 0xAC, 0x46, 0xC2, 0x01, 0xE1, 0xE0, 0x7E, 0xFB, 0x6A, 0x8D,
    0x85, 0x26, 0xD0, 0x1C, 0x3D, 0x59, 0x31, 0xF1, 0x3B, 0x91, 0xA8, 0x13, 0xBA, 0x58, 0x04, 0xE6,
    0x20, 0xAA, 0xE9, 0x31, 0x21, 0xFF, 0x16, 0xBC, 0x3C, 0xCD, 0xC4, 0x52, 0x0E, 0x82, 0x1D, 0xB7,
};

static uint8_t g_checkAddVal[] = {
    0xDB, 0xBD, 0x48, 0x3E, 0x23, 0x91, 0x0B, 0x5C, 0x81, 0x60, 0x11, 0xB2, 0xC6, 0xE6, 0x16, 0x5C,
    0x97, 0x11, 0xEF, 0xC4, 0xA3, 0xB8, 0x0B, 0xE4, 0x1B, 0x7A, 0x8F, 0x4B, 0xD5, 0xB9, 0x8B, 0x35,
    0x30, 0x99, 0x9F, 0x4E, 0x45, 0xAA, 0xF2, 0xC5, 0x12, 0x4E, 0x04, 0x3F, 0x39, 0x6E, 0x49, 0x3A,
    0xB9, 0xB1, 0xBD, 0x86, 0x2E, 0xFD, 0xA7, 0x22, 0x46, 0x86, 0x66, 0x93, 0x89, 0x93, 0x64, 0x1A,
    0x79, 0x1B, 0xD6, 0xED, 0x22, 0xC5, 0x98, 0xA4, 0x6C, 0x99, 0x96, 0x80, 0x43, 0x86, 0xC4, 0xBC,
    0x1E, 0x2A, 0x56, 0x17, 0x3A, 0x4D, 0x41, 0xFA, 0x67, 0x94, 0x36, 0x77, 0xEA, 0x7E, 0x45, 0x67,
    0x36, 0x66, 0x76, 0x1B, 0x4C, 0xE9, 0x92, 0xE4, 0xD1, 0xE6, 0xB7, 0xD3, 0xF4, 0x95, 0xBE, 0x7E,
    0xE8, 0xE3, 0x81, 0xC1, 0xBF, 0x29, 0xF9, 0xB8, 0xED, 0xA5, 0x33, 0xF8, 0x12, 0x86, 0x9C, 0x72,
    0xAB, 0x5D, 0xBD, 0x6F, 0xE4, 0x0E, 0x8E, 0x64, 0xC2, 0x33, 0x49, 0x0D, 0xF0, 0xD8, 0x16, 0xAD,
    0x70, 0x54, 0xAF, 0x85, 0xD9, 0x11, 0xBD, 0xE3, 0xEB, 0x8B, 0x46, 0x38, 0x25, 0x19, 0xD2, 0x49,
    0x5A, 0xA4, 0x0A, 0xDE, 0xEC, 0x83, 0x05, 0x1A, 0xCD, 0x49, 0x4E, 0xED, 0x37, 0x8E, 0xB8, 0xCF,
    0x3C, 0xD5, 0x5D, 0x85, 0x3B, 0xD5, 0x08, 0x02, 0x13, 0x1A, 0x05, 0x08, 0xBE, 0x5E, 0x1D, 0xAE,
    0xAE, 0x94, 0x8C, 0xAE, 0xD7, 0x48, 0x24, 0x4F, 0x8A, 0x65, 0x6D, 0x04, 0x7E, 0xB6, 0x06, 0x67,
    0x10, 0x92, 0x1C, 0x45, 0x00, 0x92, 0x0C, 0x3D, 0xD8, 0xBC, 0x53, 0x9E, 0x14, 0x58, 0xF4, 0x82,
    0x86, 0xA5, 0xD1, 0x07, 0x6D, 0xDA, 0x54, 0x15, 0x09, 0xB3, 0x11, 0xBE, 0xD1, 0x14, 0x2A, 0x7E,
    0xAE, 0xFD, 0x1B, 0x37, 0xE2, 0x21, 0xC4, 0xA6, 0x86, 0xC7, 0x8F, 0x32, 0x81, 0xF3, 0x20, 0x5C,
};

static uint8_t g_shortVal1[] = {
    0x60, 0x13, 0x16, 0xE1, 0xFB, 0xFF, 0x59, 0xF9, 0xC6, 0x15, 0x45, 0xCA, 0x9C, 0xE2, 0x10, 0xA1,
    0xC1, 0xE6, 0xC1, 0xFB, 0x4A, 0xA3, 0x14, 0xCD, 0x76, 0xDE, 0x45, 0xB7, 0x99, 0xB5, 0x56, 0x9B,
    0xCC, 0x39, 0xA4, 0x09, 0x09, 0x16, 0x51, 0xB7, 0x74, 0x9C, 0xCE, 0x11, 0x36, 0xFE, 0x37, 0xAC,
    0xF1, 0x02, 0xC4, 0x5B, 0x8E, 0x7A, 0x81, 0x6E, 0x36, 0x85, 0x9B, 0x68, 0x73, 0x85, 0x96, 0xD8,
    0x74, 0x9C, 0xEA, 0x11, 0x53, 0xDF, 0xB4, 0x59, 0xAF, 0x90, 0x71, 0x32, 0x4A, 0x16, 0xF3, 0x7B,
    0x45, 0x3B, 0xF7, 0x53, 0x3B, 0x72, 0x1D, 0xA5, 0xB9, 0xB9, 0x89, 0x63, 0x7E, 0x44, 0x5A, 0xB1,
    0x1A, 0x42, 0x28, 0x0E, 0x85, 0xB1, 0x44, 0x86, 0xB0, 0x94, 0x0E, 0x4A, 0xCE, 0xF6, 0x5A, 0xB8,
    0x4D, 0xAA, 0x49, 0xE4, 0x92, 0xF4, 0x0C, 0x92, 0xCF, 0x7C, 0x4D, 0xE0, 0xF6, 0xDA, 0x7A, 0xCF
};

static uint8_t g_shortVal2[] = {
    0x46, 0x6B, 0xA2, 0xBA, 0xA0, 0xE2, 0x03, 0x91, 0x67, 0xF8, 0x7C, 0x69, 0x04, 0x9B, 0x23, 0xF3,
    0xAE, 0x2A, 0x06, 0x2D, 0x5F, 0x2B, 0x76, 0x19, 0xCC, 0x19, 0xDD, 0xCB, 0x57, 0x63, 0x58, 0x2A,
    0xE7, 0xB9, 0x42, 0xB0, 0x33, 0x33, 0xA6, 0x17, 0x0B, 0xE4, 0x4B, 0xC3, 0x70, 0x7C, 0x74, 0xFF,
    0x11, 0xEA, 0xDE, 0x5A, 0x5B, 0xD1, 0x2C, 0xBD, 0x48, 0x8E, 0x85, 0xBB, 0x54, 0xEC, 0xBC, 0x6E,
    0x73, 0x25, 0xF2, 0x58, 0xE3, 0x05, 0x49, 0x31, 0x3E, 0x95, 0xF4, 0x85, 0xAE, 0xD4, 0xB4, 0x51,
    0xA7, 0xD3, 0xC1, 0x4E, 0x1A, 0xAF, 0x83, 0xEE, 0xC1, 0x35, 0xF8, 0x74, 0xF5, 0x33, 0x0C, 0x2E,
    0x32, 0xD2, 0x5A, 0x8E, 0x4A, 0x78, 0x07, 0xD9, 0x53, 0x65, 0x7D, 0xCA, 0x9A, 0xEB, 0xBB, 0x94,
    0x78, 0xC2, 0x2B, 0x8F, 0xF8, 0x03, 0x37, 0x44, 0x1A, 0x58, 0xE2, 0xE0, 0xA8, 0xD6, 0xEE, 0x8D
};

static uint8_t g_shortVal3[] = {
    0x22, 0x4A, 0x5A, 0xD4, 0x82, 0x4E, 0xB4, 0x91, 0xEC, 0xF2, 0xC7, 0xD6, 0x3F, 0xA0, 0x7D, 0x86,
    0x42, 0xFB, 0x22, 0xFD, 0xEE, 0x1F, 0xC8, 0xCC, 0x3F, 0x75, 0x79, 0x3A, 0x96, 0x1D, 0x07, 0x53,
    0xF9, 0xD9, 0x63, 0xEE, 0x4A, 0xD9, 0xEF, 0xF8, 0x5B, 0x58, 0x4B, 0xCE, 0xD9, 0xB6, 0xE3, 0xB6,
    0x31, 0xCD, 0x18, 0x61, 0x3F, 0x48, 0x9A, 0xF2, 0x5D, 0x09, 0x0D, 0x30, 0x3F, 0xDF, 0xAF, 0x51,
    0xAE, 0xE5, 0x13, 0xAC, 0xC6, 0xC5, 0x3D, 0x73, 0xDF, 0xFF, 0x16, 0x4F, 0x2D, 0xF0, 0x98, 0x92,
    0xB0, 0x2B, 0x14, 0x1D, 0x60, 0xB1, 0xF6, 0x60, 0x6B, 0x13, 0x69, 0xE5, 0xD0, 0x01, 0xFA, 0x2D,
    0xA0, 0xC8, 0x0A, 0x21, 0x27, 0xD6, 0x89, 0x31, 0x99, 0x2C, 0x2C, 0x36, 0x6B, 0x17, 0x04, 0xFF,
    0xE8, 0x8A, 0xB8, 0xE0, 0x38, 0x70, 0x85, 0xF9, 0x87, 0xF8, 0x4A, 0xE7, 0x3B, 0xA9, 0x3C, 0x0A
};

static uint8_t g_checkMulVal[] = {
    0x1a, 0x6D, 0x9D, 0x4E, 0x7A, 0x16, 0x88, 0x7B, 0xD2, 0x20, 0xA5, 0xCD, 0xF8, 0x26, 0x98, 0x73,
    0x10, 0x8C, 0x8D, 0x58, 0xFC, 0xFD, 0x9B, 0x22, 0x88, 0xF2, 0x96, 0xBB, 0xC8, 0x43, 0x6A, 0x90,
    0xE1, 0x72, 0xAE, 0x99, 0x27, 0xEC, 0x03, 0x2F, 0x99, 0x0D, 0x26, 0xF6, 0x33, 0xD1, 0x62, 0xD6,
    0x27, 0x33, 0x04, 0x61, 0x48, 0xF2, 0x30, 0x18, 0x56, 0x81, 0x5D, 0x35, 0x26, 0x0D, 0x1C, 0x0D,
    0x29, 0xE9, 0x8B, 0xC5, 0x6F, 0x71, 0xF3, 0x35, 0xB9, 0x7E, 0xA0, 0x00, 0xEE, 0x66, 0x16, 0x1C,
    0x41, 0x18, 0x02, 0x29, 0xE8, 0x07, 0x88, 0xBE, 0x5B, 0x06, 0x10, 0xAC, 0x74, 0x30, 0x30, 0xA2,
    0x00, 0xE5, 0x66, 0x90, 0xD7, 0xDB, 0xB3, 0x64, 0xF3, 0x8A, 0x04, 0x14, 0x67, 0x1A, 0x99, 0x1B,
    0x5C, 0x8C, 0x9C, 0x50, 0x18, 0x1E, 0x99, 0x7A, 0x1A, 0xAC, 0x43, 0x6A, 0x77, 0x54, 0x83, 0x88,
    0x0F, 0xDF, 0xC0, 0xE3, 0x53, 0xEB, 0x2F, 0xE5, 0x0D, 0x0D, 0x5C, 0x2E, 0xB9, 0xA6, 0xD0, 0x59,
    0xEC, 0x69, 0xE6, 0x38, 0x42, 0xB3, 0xFD, 0xA7, 0xF0, 0xD0, 0xD6, 0xC3, 0x03, 0xA5, 0x6F, 0x50,
    0x35, 0x5A, 0xAB, 0xAE, 0x01, 0xE8, 0x1D, 0xCF, 0x4B, 0x4B, 0x12, 0x17, 0x11, 0xD7, 0xD8, 0x8A,
    0x99, 0x99, 0xA3, 0x09, 0x47, 0x99, 0x0B, 0x02, 0x47, 0x32, 0xBE, 0xDB, 0x79, 0x7E, 0x40, 0xE9,
    0xE2, 0x26, 0x3D, 0xA7, 0x33, 0xB0, 0x04, 0xD4, 0x28, 0x9A, 0xB6, 0x42, 0x39, 0x21, 0x35, 0xA1,
    0x19, 0x28, 0x76, 0x0C, 0x7E, 0xC8, 0xC5, 0x28, 0x13, 0x01, 0x16, 0x7B, 0xD5, 0x88, 0x37, 0xA2,
    0x31, 0x1B, 0x9B, 0x05, 0x42, 0x05, 0x7A, 0xE4, 0xF5, 0xF6, 0x9A, 0xFE, 0x5C, 0xCA, 0x70, 0xDE,
    0x7B, 0x3E, 0xB0, 0xFF, 0x11, 0xC2, 0x51, 0x1A, 0xB0, 0xA9, 0xF8, 0xC1, 0x95, 0x8C, 0x16, 0x03
};

TEE_Result TestBigIntAddAndSub()
{
    tlogi("[%s] begin:", __FUNCTION__);
    TEE_Result ret = TEE_SUCCESS;
    TEE_BigInt *op1 = CreateBigInt(sizeof(g_longVal1), g_longVal1);
    TEE_BigInt *op2 = CreateBigInt(sizeof(g_longVal2), g_longVal2);

    TEE_BigInt *addDest = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *addCheck = CreateBigInt(sizeof(g_checkAddVal), g_checkAddVal);
    TEE_BigIntAdd(addDest, op1, op2);
    if (TEE_BigIntCmp(addDest, addCheck) != 0) {
        tloge("BigIntAdd failed.");
        ret = TEE_ERROR_GENERIC;
        goto CLEANUP;
    }

    TEE_BigInt *subDest = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigIntSub(subDest, addDest, op1);
    if (TEE_BigIntCmp(subDest, op2) != 0) {
        tloge("BigIntSubMod failed.");
        ret = TEE_ERROR_GENERIC;
    }
    TEE_Free(subDest);

CLEANUP:
    TEE_Free(op1);
    TEE_Free(op2);
    TEE_Free(addCheck);
    TEE_Free(addDest);
    tlogi("[%s] end. ret = 0x%x.", __FUNCTION__, ret);
    return ret;
}

TEE_Result TestBigIntNeg()
{
    tlogi("[%s] begin:", __FUNCTION__);
    TEE_Result ret = TEE_SUCCESS;
    TEE_BigInt *op1 = CreateBigInt(sizeof(g_longVal1), g_longVal1);
    TEE_BigInt *negDest = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *addForNegCheck = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigIntNeg(negDest, op1);
    TEE_BigIntAdd(addForNegCheck, negDest, op1);
    if (TEE_BigIntCmpS32(addForNegCheck, 0) != 0) {
        tloge("BigIntNeg failed.");
        ret = TEE_ERROR_GENERIC;
    }

    TEE_Free(op1);
    TEE_Free(negDest);
    TEE_Free(addForNegCheck);
    tlogi("[%s] end. ret = 0x%x.", __FUNCTION__, ret);
    return ret;
}

TEE_Result TestBigIntMulAndSquare()
{
    tlogi("[%s] begin:", __FUNCTION__);
    TEE_Result ret = TEE_SUCCESS;
    TEE_BigInt *op1 = CreateBigInt(sizeof(g_shortVal1), g_shortVal1);
    TEE_BigInt *op2 = CreateBigInt(sizeof(g_shortVal2), g_shortVal2);

    TEE_BigInt *mulDest = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *mulCheck = CreateBigInt(sizeof(g_checkMulVal), g_checkMulVal);
    TEE_BigIntMul(mulDest, op1, op2);
    if (TEE_BigIntCmp(mulDest, mulCheck) != 0) {
        tloge("BigIntMul failed.");
        ret = TEE_ERROR_GENERIC;
        goto CLEANUP;
    }

    TEE_BigInt *squareDest = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *mulForsquareCheck = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigIntSquare(squareDest, op1);
    TEE_BigIntMul(mulForsquareCheck, op1, op1);
    if (TEE_BigIntCmp(squareDest, mulForsquareCheck) != 0) {
        tloge("BigIntSquare failed.");
        ret = TEE_ERROR_GENERIC;
    }
    TEE_Free(squareDest);
    TEE_Free(mulForsquareCheck);

CLEANUP:
    TEE_Free(op1);
    TEE_Free(op2);
    TEE_Free(mulDest);
    TEE_Free(mulCheck);
    tlogi("[%s] end. ret = 0x%x.", __FUNCTION__, ret);
    return ret;
}

TEE_Result TestBigIntDiv()
{
    tlogi("[%s] begin:", __FUNCTION__);
    TEE_Result ret = TEE_SUCCESS;
    TEE_BigInt *op1 = CreateBigInt(sizeof(g_shortVal1), g_shortVal1);
    TEE_BigInt *op2 = CreateBigInt(sizeof(g_shortVal2), g_shortVal2);
    TEE_BigInt *op3 = CreateBigInt(sizeof(g_shortVal3), g_shortVal3);

    TEE_BigInt *mulDest = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *dividend = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *destR = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *destQ = CreateBigInt(RESULT_SIZE, 0);

    TEE_BigIntMul(mulDest, op1, op2);
    TEE_BigIntAdd(dividend, mulDest, op3); // dividend = (op1*op2) + op3
    TEE_BigIntDiv(destQ, destR, dividend, op1);

    if (TEE_BigIntCmp(destQ, op2) != 0 || TEE_BigIntCmp(destR, op3) != 0) {
        tloge("TEE_BigIntDiv failed.");
        ret = TEE_ERROR_GENERIC;
    }

    TEE_Free(op1);
    TEE_Free(op2);
    TEE_Free(op3);
    TEE_Free(mulDest);
    TEE_Free(dividend);
    TEE_Free(destR);
    TEE_Free(destQ);
    tlogi("[%s] end. ret = 0x%x.", __FUNCTION__, ret);
    return ret;
}
